package com.itheima.reggie.controller;


import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;


    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R submit(@RequestBody Orders orders){
        log.info("订单数据：{}",orders);
        ordersService.submit(orders);
        return R.success("下单成功");
    }



    @GetMapping("/userPage")
    public R orderPage(int page,int pageSize){
        Page<Orders> ordersPage = new Page<>(page,pageSize);
        Long userId = UserHolder.getUserId();
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Orders::getUserId,userId);
        Page<Orders> ordersPage1 = ordersService.page(ordersPage, lqw);

        return R.success(ordersPage1);
    }


    @GetMapping("/page")
    public R dingdan(Integer page, Integer pageSize, Long number){

        Page pageIof = new Page<>(page,pageSize);

        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(number != null, Orders::getNumber, number);
        wrapper.orderByDesc(Orders::getCheckoutTime);
        ordersService.page(pageIof,wrapper);
        return R.success(pageIof);

    }

}
